package cn.edu.laud.sany.datatest;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.List;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.CfDef;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.KeySlice;
import org.apache.cassandra.thrift.KsDef;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.SchemaDisagreementException;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class CliUtils
{
	public static void main(String[] args) throws TException,
			InvalidRequestException, UnavailableException,
			UnsupportedEncodingException, NotFoundException, TimedOutException,
			SchemaDisagreementException
	{
		TTransport tr = new TFramedTransport(
				new TSocket("166.111.80.203", 9160));
		// TTransport tr = new TFramedTransport(new TSocket("localhost", 9170));
		TProtocol proto = new TBinaryProtocol(tr);
		Cassandra.Client client = new Cassandra.Client(proto);
		tr.open();
		String keyspace = "laud_sany_case91";
		String columnfamilyString = null;
		KsDef ksDef = null;
		List<CfDef> cfDefs = null;
		String filepath = "keys";
		File file = new File(filepath);
		file.mkdirs();
		try
		{
			ksDef = client.describe_keyspace(keyspace);
			cfDefs = ksDef.cf_defs;
		}
		catch (NotFoundException e)
		{
			System.out.println(e.getMessage());
			return;
		}
		for (CfDef cfDef : cfDefs)
		{
			columnfamilyString = cfDef.getName();
			System.out.println(columnfamilyString + "...");
			client.set_keyspace(keyspace);
			ColumnParent parent = new ColumnParent(columnfamilyString);
			// // read single column
			// path.setColumn(toByteBuffer("name"));
			// System.out.println(client.get(toByteBuffer(key_user_id), path,
			// ConsistencyLevel.ONE));
			
			// // read entire row
			// SlicePredicate predicate = new SlicePredicate();
			// SliceRange sliceRange = new SliceRange(toByteBuffer(""),
			// toByteBuffer(""), false, 10);
			// predicate.setSlice_range(sliceRange);
			//
			// List<ColumnOrSuperColumn> results =
			// client.get_slice(toByteBuffer(key_user_id), parent, predicate,
			// ConsistencyLevel.ONE);
			
			// Get all keys
			KeyRange keyRange = new KeyRange(10000000);
			keyRange.setStart_key(new byte[0]);
			keyRange.setEnd_key(new byte[0]);
			SlicePredicate predicate = new SlicePredicate();
			SliceRange sliceRange = new SliceRange(toByteBuffer(1),
					toByteBuffer(2), false, 1);
			predicate.setSlice_range(sliceRange);
			
			List<KeySlice> results = client.get_range_slices(parent, predicate,
					keyRange, ConsistencyLevel.ONE);
			file = new File(filepath + "/" + columnfamilyString);
			FileWriter writer = null;
			int i = 0;
			try
			{
				writer = new FileWriter(file);
				for (KeySlice keySlice : results)
				{
					writer.write(toString(keySlice.key) + "\n");
					i++;
					if (i % 1000 == 0)
					{
						System.out.print(".");
						if (i % 100000 == 0)
						{
							System.out.println();
						}
						
					}
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			finally
			{
				try
				{
					writer.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				System.out.println();
			}
		}
		
		tr.close();
	}
	
	private static ByteBuffer toByteBuffer(float f)
	{
		// TODO Auto-generated method stub
		return ByteBufferUtil.bytes(f);
	}
	
	private static ByteBuffer toByteBuffer(long l)
	{
		// TODO Auto-generated method stub
		return ByteBufferUtil.bytes(l);
	}
	
	public static ByteBuffer toByteBuffer(String value)
			throws UnsupportedEncodingException
	{
		return ByteBuffer.wrap(value.getBytes("UTF-8"));
	}
	
	public static String toString(ByteBuffer buffer)
			throws UnsupportedEncodingException
	{
		byte[] bytes = new byte[buffer.remaining()];
		buffer.get(bytes);
		return new String(bytes, "UTF-8");
	}
	
	public static long toLong(ByteBuffer buffer)
			throws UnsupportedEncodingException
	{
		return ByteBufferUtil.toLong(buffer);
	}
	
	public static String[] equips;
	static
	{
		equips = new String[283];
		for (int i = 0; i < 283; i++)
		{
			String hex = Integer.toHexString(i);
			if (hex.length() == 1)
			{
				hex = hex + hex;
			}
			equips[i] = hex + hex + hex + hex + hex;
		}
	}
	public static int[] paraIds;
	static
	{
		paraIds = new int[250];
		for (int i = 0; i < 250; i++)
		{
			paraIds[i] = 500 + i;
		}
	}
}